// pages/classification/classification.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menus: [
    ],
    muchs: {
      id: -1,
      goodsName: "更多",
      picture: false
    },
     classifys: 1,
    current: 1,
    hidden: true,
    isMore: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      imageUrl: app.globalData.imageUrl
    })
    app.showShareMenus();
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onShareAppMessage: function (res) {
    return {
      title: '农产品配送商城Lite',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var openid = app.globalData.navToMenusType;
    //console.log("openid" + openid);
    if (openid) {
      if (openid < 5) {
        that.setData({
          current: openid,
        })
      } else {
        that.setData({
          current: 5,
          openid: openid
        })
      }
    }
    getClassify(this)
  },
  getmore: function () {
    this.setData({
      hidden: !this.data.hidden,
      current: 5
    })
  },
  getMoreClassifys: function (e) {
    this.setData({
      hidden: true
    })
    getBackData2(this, e.target.id)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  getClassifyProduct: function (e) {
    this.setData({
      current: e.target.dataset.current,
      isMore: false,
      hidden: true
    })
    if (this.data.menus[this.data.current - 1].picture) {
      //console.log("sdfffff");
    } else {
      getBackData(this, this.data.current)
    }
  }
})

function getBackData(that, classifys) {//从后台获取直营普通商品
  classifys = that.data.current;
  that.data.menus[classifys - 1].picture = true;
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
      pageNum: 1,
      pageCount: 100,
       classify: classifys,
      putaway: 1,
      // type:0//加载普通商品0，由于前期商品少，故注释
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      that.data.menus[classifys - 1].content = res.data.list;
      if (res.data && res.data.list.length > 0) {
        that.setData({
          menus: that.data.menus,
        })
      }
    }
  })
}

function getClassify(that) {
  wx.request({
    url: app.globalData.allUrl + "/superMarketAction/getGoodsType",
    data: {
      // type:0//加载普通商品0，由于前期商品少，故注释
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      var gg = {};
      var muchClassify = [];
      if (res.data) {
        var cc = res.data.shift();
        var bb = res.data.pop();
        for (var a = 0; a < res.data.length; a++) {
          if (a == 0) {
            res.data[a].picture = false;
          } else {
            res.data[a].picture = false;
          }
        }
        if (res.data.length > 5) {
          console.log(a);
          muchClassify = res.data.splice(4, res.data.length - 4);
          res.data.push(that.data.muchs)
        }
        that.setData({
          menus: res.data,
          muchClassify: muchClassify,
          isMore: false,
        })
        if (that.data.current < 5) {
          console.log();
          getBackData(that)//获取普通商品
        } else {
          getBackData2(that, that.data.openid)
        }

      }

    }
  })
}

function getBackData2(that, classifys) {//从后台获取直营普通商品
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
      pageNum: 1,
      pageCount: 1000,
      classify: classifys,
      putaway: 1,

      // type:0//加载普通商品0，由于前期商品少，故注释
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      that.setData({
        isMore: true,
        moreChooseProduct: res.data.list
      })
    }
  })
}